package com.umeox.moto.watch.fitness.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.watch.fitness.R;
import com.umeox.moto.watch.fitness.db.model.TodayChallenge;

import java.util.List;
import java.util.Locale;

public class ChallengeAdapter extends RecyclerView.Adapter<ChallengeAdapter.Holder> {

    public static final int TYPE_WALK = 1;
    public static final int TYPE_RUN = 11;
    public static final int TYPE_SWIM = 12;
    public static final int TYPE_CYCLE = 13;
    public static final int TYPE_FOOTBALL = 21;
    public static final int TYPE_GYM = 22;
    public static final int TYPE_JUMPROPE = 23;
    public static final int TYPE_YOGA = 24;

    public List<TodayChallenge> getDataList() {
        return mDataList;
    }

    private List<TodayChallenge> mDataList;

    private Context mContext;
    private OnItemClickListener mOnItemClickListener;


    public ChallengeAdapter(Context context,List<TodayChallenge> datas) {
        mContext = context;
        mDataList = datas;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_challenge_layout, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        Holder viewHolder = (Holder) holder;
        viewHolder.bind(mDataList.get(position));
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setDataList(List<TodayChallenge> dataList) {
        mDataList.clear();
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }


    protected class Holder extends RecyclerView.ViewHolder {
        private ImageView mTypeIv;
        private TextView mChallengeLevelTv;
        private TextView mChallengeDescTv;
        private TextView mRewardsTv;
        private View mItemView;


        public Holder(@NonNull View itemView) {
            super(itemView);
            mTypeIv = itemView.findViewById(R.id.iv_type);
            mChallengeLevelTv = itemView.findViewById(R.id.tv_challenge_level);
            mChallengeDescTv = itemView.findViewById(R.id.tv_challenge_desc);
            mRewardsTv = itemView.findViewById(R.id.tv_rewards);
            mItemView = itemView;
        }


        public void bind(TodayChallenge challengeBean) {

            int resId = R.drawable.ic_walk;
            String challengeLevelStr = mContext.getResources().getString(R.string.easy_str);
            String challengeDescStr = mContext.getResources().getString(R.string.walk_for_str);

            switch (challengeBean.sportType){
                case TYPE_WALK:
                    resId = R.drawable.ic_walk;
                    challengeLevelStr = mContext.getResources().getString(R.string.easy_str);
                    challengeDescStr = mContext.getResources().getString(R.string.walk_for_str);
                   break;
                case TYPE_RUN:
                    resId = R.drawable.ic_run;
                    challengeLevelStr = mContext.getResources().getString(R.string.medium_str);
                    challengeDescStr = mContext.getResources().getString(R.string.run_for_str);
                    break;
                case TYPE_SWIM:
                    resId = R.drawable.ic_swim;
                    challengeLevelStr = mContext.getResources().getString(R.string.medium_str);
                    challengeDescStr = mContext.getResources().getString(R.string.swim_for_str);
                    break;
                case TYPE_CYCLE:
                    resId = R.drawable.ic_ride;
                    challengeLevelStr = mContext.getResources().getString(R.string.medium_str);
                    challengeDescStr = mContext.getResources().getString(R.string.cycle_for_str);
                    break;
                case TYPE_FOOTBALL:
                    resId = R.drawable.ic_football;
                    challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                    challengeDescStr = mContext.getResources().getString(R.string.football_for_str);
                    break;
                case TYPE_GYM:
                    resId = R.drawable.ic_gym;
                    challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                    challengeDescStr = mContext.getResources().getString(R.string.gym_for_str);
                    break;
                case TYPE_JUMPROPE:
                    resId = R.drawable.ic_jump;
                    challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                    challengeDescStr = mContext.getResources().getString(R.string.jump_rope_for_str);
                    break;
                case TYPE_YOGA:
                    resId = R.drawable.ic_yoga;
                    challengeLevelStr = mContext.getResources().getString(R.string.hard_str);
                    challengeDescStr = mContext.getResources().getString(R.string.yoga_rope_for_str);
                    break;
            }

            mTypeIv.setImageResource(resId);
            mChallengeLevelTv.setText(challengeLevelStr);
            mChallengeDescTv.setText(String.format(Locale.getDefault(),challengeDescStr,challengeBean.duration));
            mRewardsTv.setText(String.format(Locale.getDefault(),"+ %d",challengeBean.rewardValue));

            mItemView.setOnClickListener(v -> {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(challengeBean);//PTT
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(TodayChallenge challengeBean);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
